package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.clinic.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/3/12 13:09
 */
public class UserClinicDaoImpl extends TempDataObjectDao<UserClinicData> {
    public static String insertSql = "insert into t_u_clinic_data(userId, unLockPatientList, waitGetPatientList, curEvenId, eventNpc, waitEventList, eventCompeteNum," +
            "clinicRewardData, patientNum, lastGetPatientTime, lastCuredTime, curReceptPatientNum, waitCurePatientList, patientClientStates, curedPatientCount, clinicScore," +
            "maxPatientId, rankActivityDataMap) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserClinicData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getUnLockPatientList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWaitGetPatientList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurEvenId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventNpc()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWaitEventList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventCompeteNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getClinicRewardData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatientNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastGetPatientTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastCuredTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurReceptPatientNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWaitCurePatientList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getPatientClientStates())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCuredPatientCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getClinicScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxPatientId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getRankActivityDataMap())));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_clinic_data SET unLockPatientList=?, waitGetPatientList=?, curEvenId=?, eventNpc=?, waitEventList=?," +
            "eventCompeteNum=?, clinicRewardData=?, patientNum=?, lastGetPatientTime=?, lastCuredTime=?, curReceptPatientNum=?, waitCurePatientList=?," +
            "patientClientStates=?, curedPatientCount=?, clinicScore=?, maxPatientId=?, rankActivityDataMap=? WHERE userId = ?";
    @Override
    protected boolean updateData(UserClinicData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getUnLockPatientList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWaitGetPatientList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurEvenId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventNpc()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWaitEventList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventCompeteNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getClinicRewardData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatientNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastGetPatientTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastCuredTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurReceptPatientNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWaitCurePatientList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getPatientClientStates())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCuredPatientCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getClinicScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxPatientId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getRankActivityDataMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserClinicData parseData(ResultSet rs) throws SQLException {
        UserClinicData data = new UserClinicData();
        data.setUserId(rs.getLong("userId"));
        data.setUnLockPatientList(JSONObject.parseObject(rs.getString("unLockPatientList"), new TypeReference<List<Integer>>() {
        }));
        data.setWaitGetPatientList(JSONObject.parseObject(rs.getString("waitGetPatientList"), new TypeReference<List<Integer>>() {
        }));
        data.setCurEvenId(rs.getInt("curEvenId"));
        data.setEventNpc(rs.getInt("eventNpc"));
        data.setWaitEventList(JSONObject.parseObject(rs.getString("waitEventList"), new TypeReference<List<Integer>>() {
        }));
        data.setEventCompeteNum(rs.getInt("eventCompeteNum"));
        data.setClinicRewardData(JSONObject.parseObject(rs.getString("clinicRewardData"), new TypeReference<ClinicRewardData>() {
        }));
        data.setPatientNum(rs.getInt("patientNum"));
        data.setLastGetPatientTime(rs.getLong("lastGetPatientTime"));
        data.setLastCuredTime(rs.getLong("lastCuredTime"));
        data.setCurReceptPatientNum(rs.getInt("curReceptPatientNum"));
        data.setWaitCurePatientList(JSONObject.parseObject(rs.getString("waitCurePatientList"), new TypeReference<List<WaitCurePatient>>() {
        }));
        data.setPatientClientStates(JSONObject.parseObject(rs.getString("patientClientStates"), new TypeReference<List<PatientClientState>>() {
        }));
        data.setCuredPatientCount(rs.getInt("curedPatientCount"));
        data.setClinicScore(rs.getLong("clinicScore"));
        data.setMaxPatientId(rs.getLong("maxPatientId"));
        String rsString = rs.getString("rankActivityDataMap");
        if (rsString == null || rsString.isEmpty()) {
            data.setRankActivityDataMap(new ConcurrentHashMap<>());
        } else {
            data.setRankActivityDataMap(JSONObject.parseObject(rsString, new TypeReference<ConcurrentHashMap<Integer, RankActivityData>>() {}));
        }
        return data;
    }

    public UserClinicData getUserClinicData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        UserClinicData userClinicData = null;
        String sql = "select * from t_u_clinic_data where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                userClinicData = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userClinicData;
    }
}
